package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryDuty;
import com.neusoft.neuiotms.dcam.fixed.model.QInventoryDuty;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedInventoryDutyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInventoryDutyRepositoryImpl extends AbstractRepository
        implements CustomizedInventoryDutyRepository {

    @Override
    public Page<InventoryDuty> search(final Pageable pageable, final Searchable searchable) {
        final QInventoryDuty qInventoryDuty = QInventoryDuty.inventoryDuty;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryDuty> query = this.query().selectFrom(qInventoryDuty);
        query.where(where);// .orderBy(qInventoryDuty.updateTime.desc());
        return super.search(query, pageable);
    }

    @Override
    public List<InventoryDuty> search(final Searchable searchable) {
        final QInventoryDuty qInventoryDuty = QInventoryDuty.inventoryDuty;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryDuty> query = this.query().selectFrom(qInventoryDuty);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return InventoryDuty.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QInventoryDuty qInventoryDuty = QInventoryDuty.inventoryDuty;

        // 任务名称
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.and(super.containsStr(qInventoryDuty.name, searchable, "name"));
        }

        // 任务ID
        if (null != searchable.getLongValue("id")) {
            where.and(super.equalsLong(qInventoryDuty.id, searchable, "id"));
        }

        // 任务状态
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and(super.equalsEnum(qInventoryDuty.status, searchable, "status",
                    value -> InventoryDutyStatus.valueOf(value)));
        }

        // 盘点人username
        if (null != searchable.getStrValue("account")) {
            where.and(super.equalsStr(qInventoryDuty.account, searchable, "account"));
        }

        // 盘点人名称
        if (!StringUtils.isEmpty(searchable.getStrValue("userName"))) {
            where.and(super.containsStr(qInventoryDuty.userName, searchable, "userName"));
        }

        // 计划ID
        if (null != searchable.getLongValue("planId")) {
            where.and(super.equalsLong(qInventoryDuty.inventoryPlan.id, searchable, "planId"));
        }

        // 任务状态
        if (!StringUtils.isEmpty(searchable.getStrValue("notstatus"))) {
            where.andNot(super.equalsEnum(qInventoryDuty.status, searchable, "notstatus",
                    value -> InventoryDutyStatus.valueOf(value)));
        }

        return where;
    }
}
